package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import java.util.ArrayList;

import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;
 
/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    private MainMenuUI mmu;
    //constructor which does nothing
    public AddressBookUI()
    {
       addrCtr = new AddressCtr();
       mmu = new MainMenuUI();
    }
    
    public void start()
    {
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while (!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          if (choice == 1)
          {   //show person
              getPerson();
          }
          else
          {
              if (choice == 2)
              {
                    //create person
                  AddressCtr.createPerson(this.inputPersonsID(), this.inputName(), this.inputAddress(), this.inputPostalCode(), this.inputCity(), this.inputPhone());
              }  
              else
              {
                  if (choice == 3)
                  {
                      //delete person
                      AddressCtr.deletePerson(this.inputPersonsID());
                  }  
                  else
                  {
                      if (choice == 4)
                      {
                          // update person
                          long id = this.inputPersonsID();
                          if ( AddressCtr.getPerson(id) == null )
                          {
                              System.out.println("The Person is not present in the address book");
                          }
                          else
                          {
                              AddressCtr.updatePerson(new Person(id, this.inputName(), this.inputAddress(), this.inputPostalCode(), this.inputCity(), this.inputPhone()));
                          }
                      }
                      else
                      {
                          if (choice == 5)
                          {
                              // listAllPersons();
                              printAddressBook(); 
                          }
                          else
                          {
                              if (choice == 6)
                              {
                                  //Return to main menu
                                  mmu.mainMenuStart();
                              }
                              else
                              {
                                  exit = true;
                              }//end else
                          }//end else
                      }//end else
                  }//end else
              }//end else
          }
       }//end while
    }
    private int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
        
    //the method reads id for a person and return
    // 
    private long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
            // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         
         return name;
    }
   
    private String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         
         return code;
    }
    private String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         
         return city;
    } 
    private String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         
         return phone;
    } 
     
    private String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        
        return address;
    }

    private void getPerson()
    {//is to be changed when the address book controller is completed   
        long id = inputPersonsID();
        if (AddressCtr.getPerson(id) != null)
        {
            System.out.println("Result:\nName: " + 
            AddressCtr.getPerson(id).getName() + "\nAddress: " + 
            AddressCtr.getPerson(id).getAddress() + "\nPostal Code: " +
            AddressCtr.getPerson(id).getPostalCode() + "\nCity: " +
            AddressCtr.getPerson(id).getCity() + "\nPhone: " +
            AddressCtr.getPerson(id).getPhone());
        }
        else
            System.out.println("The Person does not exist");
            
     }
    
    private void printAddressBook()
    {
        if (AddressCtr.listAllPersons() == null )
            System.out.println("There is no person in the system");
        else
        {
            System.out.println("Results:");
            for (int i=0; i<AddressCtr.listAllPersons().size(); i++)
            {
                System.out.println("ID: " + AddressCtr.listAllPersons().get(i).getPersonID() + " " + 
                "Name: " + AddressCtr.listAllPersons().get(i).getName());
            }
        }
    }
}